<template>
  <div class="login-container">
    <div class="login-form">
      <div class="left-side">
        <img src="@/assets/login_inset.png" alt="Logo" class="logo-img">
      </div>
      <div class="right-side">
        <div style="font-size: 20px;margin-bottom:30px;font-weight: bold;text-align: center">欢迎登录 {{ title }}</div>
        <el-form ref="userForm" :model="userForm" :rules="rules" label-width="80px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="userForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="userForm.password" type="password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login('userForm')" style="margin-left:20%">登录</el-button>
            <el-button type="success" @click="$router.push('/register')">前往注册</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import {setRoutes} from "@/router";

const title = process.env.VUE_APP_TITLE;

export default {
  name: "Login",
  data() {
    return {
      title,
      userForm: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名称', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 50, message: '长度不能小于6位', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    // 提交数据
    async login(formName) {
      const valid = await this.$refs[formName].validate();
      if (!valid) {
        return false;
      }
      // 身份验证
      RVerify.action(async (code) => {
        if (code !== 1)
          return
        const response = await this.request.post("/user/login", this.userForm);
        if (response.code !== '200') {
          this.$message.error(response.msg);
        } else {
          localStorage.setItem("user", JSON.stringify(response.data));
          // 保存菜单信息
          localStorage.setItem("menus", JSON.stringify(response.data.menuList));
          this.$message.success("登录成功");
          setRoutes();
          this.$router.push("/");
        }
      });
    },
    // 重置数据
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  }
}
</script>


<style scoped>
.wrapper {
  background: url("../assets/background.png");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}

.login-form {
  /*display: flex;*/
  width: 55%;
  height: 60%;
  margin: 150px auto;
  background-color: white;
  border-radius: 16px;
  overflow: hidden;
  background-color: white;
  box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
}

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh; /* 使用视口单位使容器占据整个视口高度 */
  background-color: #f0f0f0;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  background-image: url("../assets/background.png");
}

.login-form {
  display: flex;
  width: 50%;
  height: 50%;
  background-color: white;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
}

.left-side {
  width: 50%;
  height: 100%; /* 占满高度 */
  background-color: #f0f0f0; /* 仅用于示例，实际使用时可去掉 */
}

.logo-img {
  width: 100%; /* 图片宽度占满 */
  height: 100%; /* 图片高度占满 */
  object-fit: cover; /* 保持宽高比填充 */
}

.right-side {
  display: flex;
  flex-direction: column;
  justify-content: center; /* 垂直居中 */
  width: 50%;
  padding: 20px;
}
</style>
